--
-- @Author:      feilong
-- @DateTime:    2019-12-19 15:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local constant = require("constant")
local reason = require("reason")
local mail_def = require("mail_def")

local table_insert = table.insert
local table_remove = table.remove
local math_random = math.random
local math_floor = math.floor
local math_four_five = math.four_five


local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor(message_dispatch)
	self.message_dispatch = message_dispatch	

	self:register()
end

--注册本服务里的消息
function MessageHandler:register()
	--启动
	self.message_dispatch:registerSelf('start', handler(self,self.start))
	self.message_dispatch:registerSelf('stop', handler(self,self.stop))
    self.message_dispatch:registerSelf('get_state', handler(self,self.getState))
    self.message_dispatch:registerSelf('add_recharge', handler(self,self.addRecharge))

end



------------------------------------------------------------------
--cmd
------------------------------------------------------------------
function MessageHandler:start(act_id)
	print("______shouchong_start_______")
    self.act_id = act_id
	self.config = global.config	
    self.user_manager = global.user_manager
    self.desk = global.desk
    self.config:start(act_id)
    self.desk:start()

    self.act_data = self.config:getActivity() --活动配置
    self.ac_content = self.act_data.ac_content
end

function MessageHandler:stop()
    self.desk:stopTimer()
    skynet.sleep(10)    
    dbglog("_______活动关闭________",self.act_id)
    skynet.exit()
end

--活动状态
function MessageHandler:getState()

    local recharge_item = {}
    for k, item in pairs(self.ac_content.first_recharge) do 
        table_insert(recharge_item, {
            min = item[1][1],
            max = item[1][2],
            value = item[2],
        })
    end
    local vip_item = {}
    for k, item in pairs(self.ac_content.vip_list) do 
        table_insert(vip_item, {
            min = item[1][1],
            max = item[1][2],
            value = item[2] * 100, --用于客户端显示小数
        })
    end    
    local res = {
        recharge_item = recharge_item, --  
        vip_item = vip_item, --  
        act_des = self.ac_content.ac_desc, --活动描述
    }      

    return res
end

--有充值
function MessageHandler:addRecharge(msg)
    --判断是否够100，够就改变首充状态（0没有充值达100， 1充值达100，2已发送奖励
    local uid = msg.uid
    local coins = msg.coins
    local vip = msg.vip
    local server = R().basesvr({key=uid})
    print("____addRecharge___", msg, self.ac_content.first_recharge)
    local recharge_item = nil
    local first_recharge = self.ac_content.first_recharge or {}
    for k, item in pairs(first_recharge) do 
        if coins >= item[1][1] and (coins < item[1][2] or item[1][2] < 0) then 
            recharge_item = item
            break
        end
    end
    if not next(first_recharge) then 
        --配置有问题
        errlog("_____每日首充配置first_recharge有问题____")
        return 
    end
    local vip_item = {}
    local vip_list = self.ac_content.vip_list or {}
    for k, item in pairs(vip_list) do 
        if vip == item[1][1] and vip == item[1][2] then 
            vip_item = item
            break
        end
        if vip >= item[1][1] and (vip < item[1][2] or item[1][2] < 0) then 
            vip_item = item
            break
        end        
    end
    print("___每日首充_recharge_item___",uid, recharge_item)
    if recharge_item then             
        local rate = vip_item[2] or 0
        local add_coins = recharge_item[2] or 0
        add_coins = math_four_five(add_coins*(1+rate/100))
        print("___每日首充_add_coins___",uid, add_coins, rate)
        if add_coins == 0 then 
            errlog("_____首充活动配置错误__", add_coins, uid, coins, recharge_item, vip_item)
            return 
        end
        -- local user = self.user_manager:addUser(uid) 
        -- if not user then 
        --     errlog("_____用户不存在__",uid)
        --     return 
        -- end 
        --发送奖励
        -- print("__每日首充_self.add_chip__", uid, add_coins, reason.DAY_CHARGE)
        -- self.desk:addChip(user, add_coins, reason.DAY_CHARGE)
        --发送邮件
        local award_list = {
            {   
                uid = uid, 
                title = mail_def.DAY_CHARGE_TITLE,
                coins = add_coins,
                content = string.format(mail_def.DAY_CHARGE_CONTENT, add_coins/100),
                mail_type = 2,
                op_user = mail_def.OP_USER,
                reason = reason.DAY_CHARGE,
            }
        }
        self.desk:sendAwardMail(award_list)
        -- self.user_manager:removeUser(uid)
             
        --活动参与统计
        self.desk:opPartakeNum(uid, constant.DODULE_ID.DAY_RECHARGE)
    end
    --不能再领取奖励
    server:send('.msg_handler', 'update_day_charge_status', uid, 1)
end

return MessageHandler